#!/usr/bin/env python
# coding=UTF-8
import os
import logging
import logging.handlers
from optparse import OptionParser
import json
import time
import thread
import threading
import socket
import dao

def consumer(uid,pwd):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect(('47.93.90.95', 8098))
        data = json.dumps({"t":"login", "d":{"bid":"szjt", "uid":"%s" % uid,"pwd":"%s" % pwd}})
        sock.send(data+"^lckj^")
        sock.recv(1024)
        while True:
            sock.send('{"t":"hb","d":{"res":"ok","code":"0"}}^lckj^')
            sock.recv(1024)
            time.sleep(0.001)
        
    except:
        logging.error("error=================", exc_info=True)

def main():
    db = db = dao.Game_DB();
    sql = "SELECT * FROM t_user WHERE uid>100031";
    tmpres = db.query(sql)
    for user in tmpres:
        tmpuid = user['uid']
        tmppwd = user['md5pwd']
        print tmpuid
        print tmppwd
        threading.Thread(target=consumer, args=(tmpuid, tmppwd)).start()
  

class MJ():
    def get_joker_cnt(self, card, joker):
        joker_cnt = 0
        for j in joker:
            k = card.count(j)
            joker_cnt += k
        return joker_cnt
    
    def get_jiang(self, card, joker=[]):
        r = []
        joker_cnt = 0
        for j in joker:
            k = card.count(j)
            joker_cnt += k
            for i in range(k):
                card.remove(j)
        for k in set(card):
            if joker_cnt > 0:
                r.append(k)
            else:
                if card.count(k) >= 2:
                    r.append(k)
        return r

    def get_n(self, card, jiang, joker=[]):
        n = 0
        #print "===当将牌为%s时,各种排列组合为==" % jiang
        #logging.info(u"当奖牌为%s时候的组合=======" % jiang)
        res = []
        card.sort()
        
        joker_cnt = 0
        all_joker = []
        for j in joker:
            k = card.count(j)
            joker_cnt += k
            for i in range(k):
                all_joker.append(j)
                card.remove(j)
        if joker_cnt >= 4:
            return 4
        joker = all_joker
        jiang_cnt = card.count(jiang)
        card.remove(jiang)  
        if jiang_cnt < 2:
            joker_cnt -= 1
            joker.pop()
        else:
            card.remove(jiang)
        
        while card:
            c = card.pop(0)
            card_num = card.count(c) 
            if card_num >= 2:
                n += 1
                card.remove(c)
                card.remove(c)
                res.append([c, c, c])
            elif c > 40:
                if card_num + joker_cnt >= 2:
                    tmp_res = [c, ]
                    need_joker = 2 - card_num
                    n += 1
                    joker_cnt -= need_joker
                    for i in range(card_num):
                        card.remove(c)
                        tmp_res.append(c)
                    for i in range(need_joker):
                        jk = joker.pop()
                        tmp_res.append(jk)
                    res.append(tmp_res)
                else:
                    break
            elif card_num == 1:
                c2 = c + 1
                c3 = c + 2
                if joker_cnt >= 2:
                    n += 1
                    joker_cnt -= 1
                    jk = joker.pop()
                    card.remove(c)
                    res.append([c, c, jk])
                elif c2 in card and c3 in card:
                    n += 1
                    card.remove(c2)
                    card.remove(c3)
                    res.append([c, c2, c3])
                elif joker_cnt == 1:
                    if c2 in card:
                        n += 1
                        card.remove(c2)
                        joker_cnt -= 1
                        jk = joker.pop()
                        res.append([c, c2, jk])
                    elif c3 in card:
                        n += 1
                        card.remove(c3)
                        joker_cnt -= 1
                        jk = joker.pop()
                        res.append([c, jk, c3])
                    else:
                        n += 1
                        joker_cnt -= 1
                        jk = joker.pop()
                        card.remove(c)
                        res.append([c, c, jk])
                else:
                    break
            else:
                c2 = c + 1
                c2_cnt = card.count(c2)
                if c2_cnt > 1:
                    c2_cnt = 1
                c3 = c + 2
                c3_cnt = card.count(c3)
                if c3_cnt > 1:
                    c3_cnt = 1
                if c2_cnt + c3_cnt + joker_cnt >= 2:
                    n += 1
                    tmp_res = [c,]
                    need_joker = 2 - (c2_cnt + c3_cnt)
                    joker_cnt -= need_joker
                    for i in range(c2_cnt):
                        card.remove(c2)
                        tmp_res.append(c2)
                    for i in range(c3_cnt):
                        card.remove(c3)
                        tmp_res.append(c3)
                    for i in range(need_joker):
                        jk = joker.pop()
                        tmp_res.append(jk)
                    res.append(tmp_res)
                else:
                    break
        if joker_cnt % 3 == 0:
            n += (joker_cnt / 3)
        return n

    def get_max_n(self, card_str, joker=[]):
        allcard = [int(x) for x in card_str]
        ccc = [int(x) for x in card_str]
        joker = [int(x) for x in joker]
        jiang_list = self.get_jiang(allcard, joker)
        if not jiang_list:
            return 0
        #print jiang_list, '可以做将的列表'
        res = [0, ]
        for jiang in sorted(jiang_list):
            ddd = sorted(ccc)
            n = self.get_n(ddd, jiang, joker)
            res.append(n)
            print n,"**********",jiang
        return max(res) + 1

if __name__ == "__main__":
    cccc = ['11', '14', '14', '15', '16', '16', '16', '19', '21', '29', '32', '35', '36']
    mj = MJ()
    jk = '50'
    cccc.append(jk)
    joker = [jk,]
    print jk,"+++++",joker
    print mj.get_max_n(cccc, joker)
    
    